package com.onewo.arthasdemo.agent;

import com.onewo.arthasdemo.handler.Handler;
import com.onewo.arthasdemo.server.DiscardServer;

import java.lang.instrument.Instrumentation;
import java.util.logging.Logger;

public class TestAgent implements Runnable{
    private static final Logger LOG = Logger.getLogger(TestAgent.class.getName());

    /**
     * jvm 参数形式启动，运行此方法
     *
     * @param agentArgs
     * @param inst
     */
    public static void premain(String agentArgs, Instrumentation inst) {
        System.out.println("premain");
        Handler.setInst(inst);
        inst.addTransformer(new Handler(), true);
        new Thread(new TestAgent()).start();
    }

    /**
     * 动态 attach 方式启动，运行此方法
     *
     * @param agentArgs
     * @param inst
     */
    public static void agentmain(String agentArgs, Instrumentation inst) {
        System.out.println("agentmain");
        Handler.setInst(inst);
        inst.addTransformer(new Handler(), true);
        new Thread(new TestAgent()).start();
    }


    @Override
    public void run() {
        int port = 9999;
        try {
            new DiscardServer(port).start();
        } catch (Exception e) {
            LOG.warning("服务端启动失败！");
        }
    }
}
